package com.yoso.shop.interfaces.controller.user;

import com.alibaba.fastjson.JSON;
import com.yoso.base.interfaces.controller.BaseController;
import com.yoso.base.interfaces.controller.BaseResponse;
import com.yoso.base.service.IBaseService;
import com.yoso.shop.application.service.IConsumerService;
import com.yoso.shop.domain.user.ConsumerEntity;
import com.yoso.shop.domain.user.ConsumerEntityExample;

import com.yoso.shop.interfaces.vo.user.ConsumerVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author shizejin
 * @date 2021/11/17
 * @description:
 */
@RestController
@CrossOrigin
@RequestMapping("/consumer")
@Api(tags = "消费者")
public class ConsumerInfoController extends BaseController<ConsumerVo, ConsumerEntity> {
    @Autowired
    private IConsumerService consumerService;
    @Override
    public ConsumerEntity V2E(ConsumerVo data) {
        return JSON.parseObject(JSON.toJSONString(data),ConsumerEntity.class);
    }

    @Override
    public ConsumerVo E2V(ConsumerEntity data) {
        return JSON.parseObject(JSON.toJSONString(data), ConsumerVo.class);

    }

    @Override
    public IBaseService<ConsumerVo, ConsumerEntityExample> getService() {
        return this.consumerService;
    }

    /**
     * 用户注册
     */
    @ApiOperation("用户注册")
    @PostMapping("/register")
    public BaseResponse<Void> register(@RequestBody ConsumerVo model){
        boolean result = consumerService.register(model);
        return result ? this.success("注册成功！") : this.error("用户名已存在！");
    }
    /**
     * 用户注册
     */
    @ApiOperation("用户登录")
    @PostMapping("/login")
    public BaseResponse login(@RequestBody ConsumerVo model){
        ConsumerVo result = consumerService.login(model);
        return !ObjectUtils.isEmpty(result) ? this.success("登录成功！",result) : this.error("用户名或密码错误！");
    }

    @Override
    public BaseResponse saveOrUpdate(ConsumerVo vo) {
        return super.saveOrUpdate(vo);
    }
}
